package actions;

import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.ReviewerModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.ReviewFormInsert;
//import model.UniversityModel;
//import model.UserModel;

public class ReviewerSubmitReviewAction extends Action {

	private static final boolean DEBUG = true;
	//private static final int UNKNOWN = -1;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
		
		// get reviewer user info from form
		ReviewFormInsert rform = (ReviewFormInsert)form;
		String grade = rform.getGrade();
		String comment = rform.getComment();
		String first = (String)request.getSession().getAttribute("fname");
		String middle = (String)request.getSession().getAttribute("mname");
		String last = (String)request.getSession().getAttribute("lname");
		String user = request.getUserPrincipal().getName();

		
		if (DEBUG) {
			System.err.println("AddReviewerAction: grade(" + grade +")");
			System.err.println("AddReviewerAction: comment(" + comment +")");
		}

		// update the given application review
		ReviewerModel.updateReview(grade, comment, first, middle, last, user);
		
		// check for number of reviews so far
		if(ReviewerModel.getReviewCount(first, middle, last) < 3)
		{
			if(DEBUG)
				System.err.println("Less than 3 reviewers, need to recommend more");
			Vector<String> otherReviewers = ReviewerModel.getMoreReviewers(first, middle, last);
			request.setAttribute("otherReviewers", otherReviewers);
			return mapping.findForward("recommend");
		}
		else if(ReviewerModel.getReviewCount(first, middle, last) == 3)
		{
			if(DEBUG)
				System.err.println("3 reviewers found, need to make decision");
			return mapping.findForward("decision");
			
		}

		return mapping.findForward("success");
	}
	
}